/*-------------------------------------------------------------------------------------------------*/
/* Nuvoton Corporation confidential                                                    */
/*                                                                                                 */
/* Copyright (c) 2009 by Nuvoton Corporation                                           */
/* All rights reserved                                                                             */
/*                                                                                                 */
/*-------------------------------------------------------------------------------------------------*/
/* File Name:                                                                                      */
/*    VPOST_Demo_NOSD.c                                                                            */
/* Description:                                                                                    */
/*     LCD display controller test program                                                                      */
/* History:                                                                                        */
/*                                                                                                 */
/* Project:                                                                                        */
/*-------------------------------------------------------------------------------------------------*/



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wblib.h"

#include "NUC900_reg.h"
#include "NUC900_vpost.h"

#define USEUART 1

#define CACHE_ON 1
#define MPU_TYPE
//#define SYNC_TYPE

#define SYSCLK 15000000

#define get_timer_ticks()	sysGetTicks(TIMER0)

#if USEUART
#undef getchar
#define getchar		sysGetChar
#define printf		sysprintf
#endif// end of #if USEUART


const UINT8 ScreenBitmap[] = {
    #include "Inc/PACKET_RGB565_size320x240_CCU.dat"
};



__align(32) UINT8 _ucOSD_buff[]=
{
	  #include "Inc/BAR_RGB565.dat"
};



__align(32) UINT32 uaCursorBuf[512];
VOID lcd_test(VOID);



INT diag_lcd(VOID)
{
    outpw(REG_CLKSEL,inpw(REG_CLKSEL) | (0x3<<6));//VCLK from EXTAL
    outpw(REG_CLKDIV,inpw(REG_CLKDIV) &~(0xf<<12) | (0x1<<12));//LCD clock = VCLK/2


/* ----------Video Initialization (Display_Continus)-----------------  */    

	//vpostVASetDisplayBuff(0x40000,0x80000);//dual buffer

	
	
	
	lcd_test();
	
	
    return 0;
}

VOID HWCursorTest()
{
    UINT32 i;
    UINT8 choose;
    UINT32 uShiftCount=0x2;
    INT32	uHCX=0,uHCY=0;
    UINT32 ScreenW,ScreenH;
    ScreenW=320;
    ScreenH=240;
    
    printf("************ User's Guide for HWCursorTest ************\n");
	printf("Describtion:Press 'w' for moving up the cursor\n");
	printf("            Press 's' for moving down the cursor\n");
	printf("            Press 'a' for moving left the cursor\n");
	printf("            Press 'd' for moving right the cursor\n");
	printf("            Press 'z' for exiting the test\n");
	printf("************                             ************\n");
    
    for (i=0;i<16;i++)
    {
    	uaCursorBuf[i] = 0x00;
        uaCursorBuf[i+16*1] = 0x55555555;
        uaCursorBuf[i+16*2] = 0xaaaaaaaa;
        uaCursorBuf[i+16*3] = 0xffffffff;
        uaCursorBuf[i+16*4] = 0x00;
        uaCursorBuf[i+16*5] = 0x55555555;
        uaCursorBuf[i+16*6] = 0xaaaaaaaa;
        uaCursorBuf[i+16*7] = 0xffffffff;
        uaCursorBuf[i+16*8] = 0x00;
        uaCursorBuf[i+16*9] = 0x55555555;
        uaCursorBuf[i+16*10] = 0xaaaaaaaa;
        uaCursorBuf[i+16*11] = 0xffffffff;
        uaCursorBuf[i+16*12] = 0x00;
        uaCursorBuf[i+16*13] = 0x55555555;
        uaCursorBuf[i+16*14] = 0xaaaaaaaa;
        uaCursorBuf[i+16*15] = 0xffffffff;
        uaCursorBuf[i+16*16] = 0x00;
        uaCursorBuf[i+16*17] = 0x55555555;
        uaCursorBuf[i+16*18] = 0xaaaaaaaa;
        uaCursorBuf[i+16*19] = 0xffffffff;
        uaCursorBuf[i+16*20] = 0x00;
        uaCursorBuf[i+16*21] = 0x55555555;
        uaCursorBuf[i+16*22] = 0xaaaaaaaa;
        uaCursorBuf[i+16*23] = 0xffffffff;
        uaCursorBuf[i+16*24] = 0x00;
        uaCursorBuf[i+16*25] = 0x55555555;
        uaCursorBuf[i+16*26] = 0xaaaaaaaa;
        uaCursorBuf[i+16*27] = 0xffffffff;
        uaCursorBuf[i+16*28] = 0x00;
        uaCursorBuf[i+16*29] = 0x55555555;
        uaCursorBuf[i+16*30] = 0xaaaaaaaa;
        uaCursorBuf[i+16*31] = 0xffffffff;
    }
    
    vpostHCInit(uaCursorBuf,HC_MODE0);
    
    do{
        choose = getchar();
        //printf("%d\n",choose);
        switch (choose)
        {
            
            case 'w':
                uHCY = uHCY - uShiftCount;
                if (uHCY<0)
                    uHCY=0;
                vpostHCPosCtrl(uHCX,uHCY);
                
                break;
            case 's':
                uHCY = uHCY + uShiftCount;
                if (uHCY>ScreenH)
                    uHCY=ScreenH-1;
                vpostHCPosCtrl(uHCX,uHCY);
                break;
            case 'a':
                uHCX = uHCX - uShiftCount;
                if (uHCX<0)
                    uHCX=0;
                vpostHCPosCtrl(uHCX,uHCY);
                break;
            case 'd':
                uHCX = uHCX + uShiftCount;
                if (uHCX>ScreenW)
                    uHCX=ScreenW-1;
                vpostHCPosCtrl(uHCX,uHCY);
                break;
            case 'z':
            return;
            default:
            break;
        }
    }while(1);
}
VOID lcd_test()
{
    UINT32 uCKey,uCMask;
    UINT8 ucWeight;
    UINT32	uOSDSrcFormat;
    int item=0;
    int	choose;
	CHAR TestItems[6]={'1','2','3','4','9','z'};
	LCDFORMATEX lcdformatex;
	OSDFORMATEX osdformatex;
	UINT32 *pframbuf=NULL;
	
#if !defined(NUC950) && !defined(NUC951)
	printf("************ User's Guide for LCD Demo ************\n");
	printf("Describtion:This demo program work with TD035TTEA3\n");
	printf("            on the NUC910 EV board.\n");
	printf("            There will be 5 tests for Video and OSD\n");
	printf("    Setting:1. LCD module TD035TTEA3 is required\n");
	printf("     Result:OK, if show the image normally\n");
    printf("            Fail, can't show the image normally\n");
	printf("************                             ************\n");
#endif
	sysprintf("\n\nPress any key to start...\n");
	sysGetChar();

	sysprintf("Video display test ...\n");
	//lcdformatex.ucVASrcFormat = VA_SRC_RGB565;
    //vpostLCMInit(&lcdformatex);
    pframbuf = vpostGetFrameBuffer();
  	if (pframbuf==NULL)
        printf("NULL FRAME BUFFER\n");
    else
        memcpy(pframbuf,ScreenBitmap,sizeof(ScreenBitmap));
	sysprintf("\n\nPress any key to continue...\n");
	sysGetChar();

	sysprintf("OSD display test ...\n");
	osdformatex.ucOSDSrcFormat = OSD_SRC_RGB565;
    osdformatex.nXstart = 50;
    osdformatex.nYstart = 50;
    osdformatex.nOSDWidth = 128;
    osdformatex.nOSDHeight = 160;
    osdformatex.nImageWidth = 128;
    osdformatex.pFrameBuffer = (PUINT32)_ucOSD_buff;
    vpostOSDInit(&osdformatex);
	sysprintf("\n\nPress any key to continue...\n");
	sysGetChar();

	sysprintf("OSD transparent test ...\n");
	if (osdformatex.ucOSDSrcFormat == OSD_SRC_RGB565)
	{
		uCKey = 0x00f80000;//key=R
		uCMask = 0x00f8fcf8;//RGB565
	}
	ucWeight = 4;
	vpostOSDSetOverlay(DISPLAY_SYNTHESIZED,DISPLAY_OSD,uCKey,uCMask,ucWeight);
	sysprintf("\n\nPress any key to continue...\n");
	sysGetChar();

	sysprintf("OSD blinking test ...\n");
	vpostOSDSetBlinking(5);
	sysprintf("\n\nPress any key to continue...\n");
	sysGetChar();

	sysprintf("Hardware Cursor test ...\n");
	HWCursorTest();
	vpostDeInit();
	vpostOSDDeinit();
	//vpostLCMDeinit();

#if 0	
    do{
		
		int STicks=0;
		
		uOSDSrcFormat = OSD_SRC_RGB565;
		
		printf("=======================================\n");
		printf("1.Video Display test\n");
		printf("2.OSD Display test\n");
		printf("3.OSD transparent test\n");
		printf("4.OSD blinking test\n");
		printf("9.Hardware Cursor test\n");
		printf("z.Exit\n");
		printf("select:");

		//gets(&choose);
		//scanf("%c\n",choose);
				
        
        choose = TestItems[item++];
		printf("Test Item %c\n",choose);
        
		switch(choose)
		{
			case '1':
				lcdformatex.ucVASrcFormat = VA_SRC_RGB565;
                vpostLCMInit(&lcdformatex);
                pframbuf = vpostGetFrameBuffer();
                if (pframbuf==NULL)
                    printf("NULL FRAME BUFFER\n");
                else
                    memcpy(pframbuf,ScreenBitmap,sizeof(ScreenBitmap));
				break;
			case '2':
				osdformatex.ucOSDSrcFormat = OSD_SRC_RGB565;
                osdformatex.nXstart = 50;
                osdformatex.nYstart = 50;
                osdformatex.nOSDWidth = 128;
                osdformatex.nOSDHeight = 160;
                osdformatex.nImageWidth = 128;
                osdformatex.pFrameBuffer = (PUINT32)_ucOSD_buff;
                vpostOSDInit(&osdformatex);
				break;
			case '3':
			
				if (osdformatex.ucOSDSrcFormat == OSD_SRC_RGB565)
				{
					uCKey = 0x00f80000;//key=R
					uCMask = 0x00f8fcf8;//RGB565
				}
				ucWeight = 4;
				vpostOSDSetOverlay(DISPLAY_SYNTHESIZED,DISPLAY_OSD,uCKey,uCMask,ucWeight);
				
			break;
			case '4':
			    vpostOSDSetBlinking(5);
			break;
			case '5':
			break;
			case '6':
			
			break;
			case '9':
			    HWCursorTest();
			break;
			break;
			case 'z':
			    //vpostLCMDeinit();

			return;
			default:break;
			
			
		}
		STicks = get_timer_ticks();
		while((get_timer_ticks() - STicks) < 300);//delay 5 sec
	}while(1);
#endif
}


